import 'package:date_format/date_format.dart';
import 'package:flutter_oldman_health/models/health_data_model.dart';

class HealthDataChartsUtils {
  String date;
  String averageStr = "";

  List<String> dateList = [];
  List<String> valueList = [];
  List<List<String>> valueList2 = [];
  List<String> valueAverageList = [];

  // Map<String, String> valueAverageMap = new Map();
  List<String> valueMaxList = [];
  List<String> valueMinList = [];

  List<List> showDayCharts(bool isTemperatureType, bool isStepHealthType,
      List<HealthItemData> serialData) {
    dateList.clear();
    valueList.clear();

    String date = "";
    for (int i = 0; i < serialData.length; i++) {
      // dateList.add("${formatDate(DateTime.fromMicrosecondsSinceEpoch(serialData[i].date), [yyyy,  "/",   mm, "/", dd  ])}");
      //要有单引号括起来
      if (!isStepHealthType) {
        date =
            "'${formatDate(DateTime.fromMillisecondsSinceEpoch(serialData[i].date), [
              HH,
              ":",
              nn,
              ":",
              ss
            ])}'";
        print(date + serialData[i].value.toStringAsFixed(0));
        // dateList.add(serialData[i].date.toString());
      } else {
        date =
            "'${formatDate(DateTime.fromMillisecondsSinceEpoch(serialData[i].date), [
              dd,
              "日"
            ])}'";
        print(date + serialData[i].value.toString());
        // dateList.add(serialData[i].date.toString());
      }
      if (!dateList.contains(date) &&
          !(serialData[i].value.toString() == "0.0")) {
        dateList.add(date);
        if (isTemperatureType) {
          valueList.add(serialData[i].value.toString());
        } else {
          valueList.add(serialData[i].value.toStringAsFixed(0));
        }
      }
    }
    return [dateList, valueList];
  }

  List<List> getAverageDataList(bool isTemperatureType,
      List<HealthAverageData> averageData, String unitX) {
    dateList.clear();
    valueList2.clear();
    valueAverageList.clear();
    valueMaxList.clear();
    valueMinList.clear();
    bool repeat = false;

    //[{inDate: 202202, average: 92.833336, min: 88.0, max: 99.0, sum: null},
    // {inDate: 202202, average: 92.833336, min: 88.0, max: 99.0, sum: null},
    // {inDate: 202201, average: 82.0, min: 82.0, max: 82.0, sum: null}]
    //需要降序的话，right.xx.compareTo(left.xx)。
    averageData.sort((left, right) => left.inDate.compareTo(right.inDate));
    for (int i = 0; i < averageData.length; i++) {
      // dateList.add(averageData[i].inDate.toString());
      String date =
          "'${(averageData[i].inDate).toString().substring(4, 6)}$unitX'";
      if (!(averageData[i].average.toString() == "0.0")) {
        repeat = dateList.contains(date);
        if (repeat) {
          valueAverageList.removeLast();
          valueMaxList.removeLast();
          valueMinList.removeLast();
        } else {
          dateList.add(date);
        }
        if (isTemperatureType) {
          // averageStr = averageData[i].average.toStringAsFixed(1);
          // if (averageStr.length == 2) {
          //   averageStr = averageStr + ".0";
          // }
          valueAverageList.add(averageData[i].average.toStringAsFixed(1));
          valueMaxList.add(averageData[i].max.toStringAsFixed(1));
          valueMinList.add(averageData[i].min.toStringAsFixed(1));
        } else {
          valueAverageList.add(averageData[i].average.toStringAsFixed(0));
          valueMaxList.add(averageData[i].max.toStringAsFixed(0));
          valueMinList.add(averageData[i].min.toStringAsFixed(0));
        }
      }
    }
    valueList2.add(valueAverageList);
    valueList2.add(valueMaxList);
    valueList2.add(valueMinList);
    return [dateList, valueList2];
  }

  List<List> getAverageDataList2(bool isTemperatureType,
      List<HealthAverageData> averageData, String unitX) {
    dateList.clear();
    valueList2.clear();
    valueAverageList.clear();
    valueMaxList.clear();
    valueMinList.clear();
    bool repeat = false;

    for (int i = 0; i < averageData.length; i++) {
      date =
          "'${DateTime.fromMillisecondsSinceEpoch(averageData[i].inDate).day}$unitX'";
      // String date = formatDate(
      //     DateTime.fromMillisecondsSinceEpoch(averageData[i].inDate),
      //     [yyyy, "-", mm, "-", dd, ""]);
      // print(date + averageData[i].average.toString());
      if (!(averageData[i].average.toString() == "0.0")) {
        repeat = dateList.contains(date);
        if (repeat) {
          valueAverageList.removeLast();
          valueMaxList.removeLast();
          valueMinList.removeLast();
        } else {
          dateList.add(date);
        }
        if (isTemperatureType) {
          //averageStr = averageData[i].average.toStringAsFixed(1);
          // print(averageStr);
          // if (averageStr.length == 2) {
          //   averageStr = "$averageStr.0";
          // }
          valueAverageList.add(averageData[i].average.toStringAsFixed(1));

          valueMaxList.add(averageData[i].max.toStringAsFixed(1));
          valueMinList.add(averageData[i].min.toStringAsFixed(1));
        } else {
          valueAverageList.add("${averageData[i].average.toStringAsFixed(0)}");
          valueMaxList.add(averageData[i].max.toStringAsFixed(0));
          valueMinList.add(averageData[i].min.toStringAsFixed(0));
        }
      }
    }
    valueList2.add(valueAverageList);
    valueList2.add(valueMaxList);
    valueList2.add(valueMinList);
    return [dateList, valueList2];
  }
}
